// zh_jdgjb/pages/interests/interests_list.js
var app = getApp();
var util = require("../../utils/util.js");
var base = require('./../../../config.js');
var WxParse = require('./../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    card_sign: 0,
    url: wx.getStorageSync("url"),
    agreement: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 头部样式
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff'
    });
    this.setData({
      card_sign: options.sign,
      originData: options
    })
    console.log(options);
    this.getData(options.id);
  },
  getData(value) {
    // value 1 已开通， 2 未开通
    let that = this;
    wx.request({
      url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=EquityCardArticle&m=zh_jdgjb&id=" + value,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data.data);
        WxParse.wxParse('privilege', 'html', res.data.data.privilege, that, 5);
        WxParse.wxParse('introduce', 'html', res.data.data.introduce, that, 5);
        that.setData({
          agreement: res.data.data.agreement
        })
      }
    });
  },
  // 跳转到协议详情页面
  agreement: function () {
    let that = this;
    wx.setStorageSync('agreement', that.data.agreement);
    wx.navigateTo({
      url: './agreement?content=' + that.data.agreement
    })
  },
  // 点击了去使用或者去开卡
  toUse: function () {
    let that = this;
    if (that.data.card_sign == 0) {
      wx.navigateBack({});
      return false;
    }
    wx.request({
      url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=AddcardOrder&m=zh_jdgjb&user_id=" + wx.getStorageSync("userInfo").id + '&equitycard_id=' + that.data.originData.id + '&price=' + that.data.originData.economy_money + '&out_trade_no=' + Date.parse(new Date()),
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {

        app.util.request({
          url: "entry/wxapp/Pay",
          cachetime: "0",
          data: {
            openid: app.OpenId,
            money: that.data.originData.economy_money,
            order_id: res.data.data.order_id,
            type: 'card'
          },
          success: function (o) {
            console.log(o);
            wx.requestPayment({
              timeStamp: o.data.timeStamp,
              nonceStr: o.data.nonceStr,
              package: o.data.package,
              signType: o.data.signType,
              paySign: o.data.paySign,
              success: function (o) {
                console.log(o);
                wx.showToast({
                  title: '支付成功'
                })
                wx.navigateBack({})
              }, fail: function () {
                wx.showToast({
                  title: '支付失败',
                  icon: 'none'
                })
                that.setData({
                  selectInter: !that.data.selectInter
                })
              }
            })
          }
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})